package com.tms.control;

import com.tms.domain.Tool;
import com.tms.service.AlgorithmService;
import com.tms.service.ToolService;
import com.tms.utils.Data;
import com.tms.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.tms.core.constants.ToolConstants.ERROR_NULL_TOOL;
import static com.tms.core.constants.ToolConstants.SUCCESS;

@RestController
@RequestMapping("/api/failure")
public class FailureController extends BaseController {

    @Autowired
    private ToolService toolService;

    @Autowired
    private AlgorithmService algorithmService;

    /**
     * 查看模具的故障概率
     *
     * @param toolId tool的id
     * @return
     */
    @GetMapping
    public JsonResult getPossibility(@RequestParam(value = "toolId") int toolId) {
        Tool tool = toolService.findToolById(toolId);
        if (tool == null) {
            return new JsonResult(ERROR_NULL_TOOL.getStatus(), ERROR_NULL_TOOL.getDesc());
        }
        double probability = algorithmService.simpleProbability(tool);
        return new JsonResult(SUCCESS.getStatus(), SUCCESS.getDesc(), new Data(probability));
    }

}